/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.dialog.navigation.view;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import offset.nodes.client.dialog.navigation.model.HeadingModel;
import offset.nodes.client.model.ServerModel;

/**
 *
 * @author Walter Lütgenau
 */
public class DebugHeadingPanel extends NavigationApplet {

    public void init() {
        ServerModel.setUserName("admin");
        ServerModel.setPassword("nodes");

        super.init();
    }

    public void initUI() {
        super.initUI();

        DebugHeadingModel headingModel = new DebugHeadingModel(this, new HeadingPanel());
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(headingModel.getPanel(), BorderLayout.CENTER);

        JButton updateButton = new JButton("Update");
        updateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });

        panel.add(updateButton, BorderLayout.SOUTH);

        models.put(headingModel.getPanel().getCardName(), headingModel);
        navigationPanel.add(panel, headingModel.getPanel().getCardName());

        navigationPanel.showCard(headingModel.getPanel().getCardName());
    }

    class DebugHeadingModel extends HeadingModel {
        class Heading {
            String heading;
            int level;

            public Heading(String heading, int level) {
                this.heading = heading;
                this.level = level;
            }

            public String getHeading() {
                return heading;
            }

            public int getLevel() {
                return level;
            }
        }
        
        Heading[][] headings = new Heading[][] {
            {
                new Heading("Very very long Page 1 1", 1),
                new Heading("Very very long Page 1 1.1", 2),
                new Heading("Very very long Page 1 1.2", 2),
                new Heading("Very very long Page 1 1.2.1", 3),
                new Heading("Very very long Page 1 1.3", 2),
                new Heading("Very very long Page 1 2", 1)
            },
            {
                new Heading("Very very long Page 2 1", 1),
                new Heading("Very very long Page 2 2", 1),
                new Heading("Very very long Page 2 2.1", 2),
                new Heading("Very very long Page 2 2.1.1", 3),
                new Heading("Very very long Page 2 2.2", 2)
            }
        };
        int page = 0;

        public DebugHeadingModel(Applet applet, HeadingPanel panel) {
            setApplet(applet);
            initPanel();
        }

        public void updatePage() throws Exception {
            HeadingPanel panel = (HeadingPanel) getPanel();
            panel.start();
            
            for (Heading heading : headings[page]) {
                panel.addHeading(null, heading.getHeading(), heading.getLevel());
            }
            page++;
            
            panel.end();
        }

        public void nodeSelected(String location) {
            System.out.println(location);
        }
    }

    public static void main(String[] arguments) {
        sun.applet.AppletViewer.main(new String[]{"src/test/java/offset/nodes/client/dialog/navigation/view/DebugHeadingPanel.html"});
    }
}
